package com.juyu.miliao.api.service;

import com.alibaba.fastjson.JSON;
import com.juyu.miliao.api.dao.mysql.OpenApiStatusDao;
import com.juyu.miliao.common.domain.CommonHead;
import com.juyu.miliao.common.domain.ConfigApp;
import com.juyu.miliao.common.domain.OpenApiStatus;
import com.juyu.miliao.common.domain.dto.OpenApiDTO;
import com.juyu.miliao.common.domain.dto.OpenApiResourceDTO;
import com.juyu.miliao.common.util.CommonUtil;
import com.juyu.miliao.common.util.VerifyUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * @author jh
 * 2018/11/13
 */
@Slf4j
@Service
public class OpenApiService {

    @Resource
    private ConfigAppService configAppService;

    @Resource
    private OpenApiStatusDao openApiStatusDao;

    /**
     * 获取 OpenApiStatus 资源存的json
     */
    public OpenApiStatus getOpenApi(CommonHead commonHead) {
        return getOpenApi(commonHead.getAppPackageId(), commonHead.getPhoneSystem(), commonHead.getVersionNum(), commonHead.getChannel());
    }

    /**
     * 获取审核状态,true 为审核状态，false为关闭审核状态
     * @param request
     * @return
     */
    public Boolean getAudit(HttpServletRequest request) {
        CommonHead commonHead = VerifyUtil.verifyCommonHead(request);
        OpenApiStatus ops = getOpenApi(commonHead);
        if(ops != null && ops.getForwardAudit() && ops.getBackAudit()){
            return true;
        }else{
            return false;
        }
    }

    /**
     * 获取 OpenApiStatus 资源存的json
     */
    public OpenApiStatus getOpenApi(String appPackageId, String phoneSystem, String versionNum, String channel) {
        if (StringUtils.isEmpty(appPackageId)) {
            return null;
        }
        if (StringUtils.isEmpty(phoneSystem)) {
            return null;
        }
        if (CommonUtil.isEmptyString(channel)) {
            return null;
        }
        if (StringUtils.isEmpty(versionNum)) {
            return null;
        }

        ConfigApp configApp = configAppService.getConfigApp(appPackageId, phoneSystem);
        if (configApp == null) {
            return null;
        }

        OpenApiStatus openApiStatus = openApiStatusDao.getOpenApiStatusByWhere(versionNum, configApp.getId(), channel);
        if (openApiStatus != null) {
            openApiStatus.setPackageGroup(configApp.getPackageGroup());
        }

        return openApiStatus;

    }

    /**
     * 前端是否审核
     */
    public Boolean getForwardAudit(CommonHead commonHead) {
        OpenApiStatus openApiStatus = getOpenApi(commonHead);
        if (openApiStatus == null) {
            return false;
        }

        return CommonUtil.null2Boolean(openApiStatus.getForwardAudit());
    }

    /**
     * 前端是否审核
     */
    public Boolean getForwardAudit(String appPackageId, String phoneSystem, String versionNum, String channel) {
        OpenApiStatus openApiStatus = getOpenApi(appPackageId, phoneSystem, versionNum, channel);
        if (openApiStatus == null) {
            return false;
        }

        return CommonUtil.null2Boolean(openApiStatus.getForwardAudit());
    }

    /**
     * 后端是否审核
     */
    public Boolean getBackAudit(CommonHead commonHead) {
        OpenApiStatus openApiStatus = getOpenApi(commonHead);
        if (openApiStatus == null) {
            return false;
        }

        return CommonUtil.null2Boolean(openApiStatus.getBackAudit());
    }

    /**
     * 后端是否审核
     */
    public Boolean getBackAudit(String appPackageId, String phoneSystem, String versionNum, String channel) {
        OpenApiStatus openApiStatus = getOpenApi(appPackageId, phoneSystem, versionNum, channel);
        if (openApiStatus == null) {
            return false;
        }

        return CommonUtil.null2Boolean(openApiStatus.getBackAudit());
    }

    /**
     * 获取 OpenApiDTO 资源存的bean
     */
    public OpenApiDTO getOpenApiDTO(CommonHead commonHead) {
        return getOpenApiDTO(commonHead.getAppPackageId(), commonHead.getPhoneSystem(), commonHead.getVersionNum(), commonHead.getChannel());
    }

    /**
     * 获取 OpenApiDTO 资源存的bean
     */
    public OpenApiDTO getOpenApiDTO(String appPackageId, String phoneSystem, String versionNum, String channel) {
        OpenApiStatus openApiStatus = getOpenApi(appPackageId, phoneSystem, versionNum, channel);
        if (openApiStatus == null) {
            return null;
        }

        OpenApiDTO dto = new OpenApiDTO();
        dto.setId(openApiStatus.getId());
        dto.setChannel(openApiStatus.getChannel());
        dto.setBackAudit(openApiStatus.getBackAudit());
        dto.setConfigAppId(openApiStatus.getConfigAppId());
        dto.setForwardAudit(openApiStatus.getForwardAudit());
        dto.setVersionNum(openApiStatus.getVersionNum());
        dto.setPackageGroup(openApiStatus.getPackageGroup());

        String backJson = openApiStatus.getBackJsonResource();
        if (!StringUtils.isEmpty(backJson)) {
            OpenApiResourceDTO apiResourceDTO = null;
            try {
                apiResourceDTO = JSON.parseObject(backJson, OpenApiResourceDTO.class);
            } catch (Exception e) {
                log.error("openApiResource 转json异常:backJson={}", backJson);
            }
            dto.setApiResourceDTO(apiResourceDTO);
        }

        return dto;
    }

}
